const {ShortMsgSendConfigModel, ShortMsgModel} = require('../../models/index')
const koaTool = require('../../commons/koa_tool')
const mqttService = require("../../services/mqtt")

class ShortMsgSendConfigController {
    create = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, ['short_msg_id', "type", 'warning_num', 'sort'])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }
        let newData = Object.assign({}, input, {
            
        })
        let detail = await ShortMsgSendConfigModel.create(newData)
        mqttService.initMsgConfigData('config');
        return koaTool.success(ctx,'创建成功');
    }

    detail = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, ['id'])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }

        const detail = await ShortMsgSendConfigModel.findById(input.id)
        koaTool.succ(ctx, {
            detail
        })
    }

  //更新
    update = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, [])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }

        const {isNull} = require("../../commons/util.js");
        if(isNull(input.id)){
            return koaTool.error(ctx,"异常");
        }

        let detail = await ShortMsgSendConfigModel.findById(input.id)
        if (!detail) {
            return koaTool.error(ctx,"数据更新异常");
        }
        const {
            short_msg_id,
            warning_num, 
            sort,  
            send_setting,
            pause_time,
            type
        } = input;


        if (!isNull(short_msg_id)) {
            detail.short_msg_id = short_msg_id
        }

        if (!isNull(type)) {
            detail.type = type
        }

        if (warning_num) {
            detail.warning_num = +warning_num
        }

        if (sort) {
            detail.sort = sort
        }

        if (send_setting) {
            detail.send_setting = send_setting
        }

        if(pause_time){
            detail.pause_time = pause_time
        }


        await detail.save()
        mqttService.initMsgConfigData('config');
        return koaTool.success(ctx, '编辑成功');
    }

    delete = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, ['id'])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }
        await ShortMsgSendConfigModel.findByIdAndRemove(input.id)
        mqttService.initMsgConfigData('config');
        koaTool.succ(ctx, {
            msg: '成功'
        })
    }

    list = async (ctx) => {
        let [checkError, input] = koaTool.checkInput(ctx, [])
        if (checkError) {
            return koaTool.fail(ctx, checkError)
        }

        let {
            page,
            limit,
            search,
        } = ctx.request.query;

        limit = Number(limit)
        page = page || 0
        page = page - 1
        let query = {  };

       
        let total = await ShortMsgSendConfigModel.countDocuments(query)
        let list = await ShortMsgSendConfigModel.find(query).sort([['sort', 1]]).skip(page * limit).limit(limit).lean();

        let templateDataList = await ShortMsgModel.find().lean();
        let templateEnum = templateDataList.reduce((a, b) => Object.assign(a, {[b._id]: b.templateName}), {});
        const {SEND_SETTING, EQUIPMENT_TYPE_MAP } = require('../../commons/const.js');
        list.forEach(item => {
            item.templateName = templateEnum[item.short_msg_id.toString()];
            item.sendSettingName = SEND_SETTING[item.send_setting]
            item.typeName = EQUIPMENT_TYPE_MAP[item.type];
        })
        // console.log("list",list)
        koaTool.succ(ctx, {
            list,
            total
        })
    }

    // 发送机制字典
    getSendSettingDict = async (ctx) => {
        const {SEND_SETTING_LIST} = require('../../commons/const.js');

        return koaTool.success(ctx,"获取成功",{
            list: SEND_SETTING_LIST
        })
    }

}

module.exports = new ShortMsgSendConfigController()
